using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace mmq.UserControls
{
    public partial class TreeEditor : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
            }
        }
        private void BindTreeView()
        {
            xmlDataSource.Data = BLL.CategoryManager.GetAllCategoriesForMenu(HttpContext.Current.Profile.GetPropertyValue("PreferredCulture").ToString());
            this.TreeView_Editor.DataSource = xmlDataSource;
            this.TreeView_Editor.DataBind();
        }
        private string navigateUrl;
        public string NavigateUrl
        {
            set
            {
                navigateUrl = value;
            }
        }


        private TreeNodeSelectAction selectAction = TreeNodeSelectAction.SelectExpand;
        public TreeNodeSelectAction SelectAction
        {
            set
            {
                selectAction = value;
            }
        }

        public string SelectedValue
        {
            get
            {
                return TreeView_Editor.SelectedValue;
            }
        }
        public string SelectedText
        {
            get
            {
                return TreeView_Editor.SelectedNode.Text;
            }
        }

        public string SelectedParentValue
        {
            get
            {
                if (TreeView_Editor.SelectedNode == null)
                    return "-1";
                if (TreeView_Editor.SelectedNode.Parent == null)
                    return "-1";
                else
                    return TreeView_Editor.SelectedNode.Parent.Value;
            }
        }

        public bool IsLeaf
        {
            get
            {
                return TreeView_Editor.SelectedNode.ChildNodes.Count < 1;
            }
        }
        public int NodesCount
        {
            get
            {
                return TreeView_Editor.Nodes.Count;
            }
        }
        public void BindData()
        {
            BindTreeView();
        }

        protected void TreeView_Editor_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
        {
            e.Node.SelectAction = selectAction;
            if (selectAction == TreeNodeSelectAction.Expand)
            {
                e.Node.NavigateUrl = string.Format(navigateUrl, e.Node.Value, e.Node.Text);
            }
        }
        public event EventHandler SelectedNodeChanged;

        protected void TreeView_Editor_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (SelectedNodeChanged != null)
                SelectedNodeChanged(sender, e);
        }

    }
}